package edu.arizona.sista.learn.model.groups;

import java.io.File;

import com.martiansoftware.jsap.FlaggedOption;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Switch;

import edu.arizona.sista.learn.model.Interval;
import edu.arizona.sista.learn.util.Utils;
import edu.arizona.sista.learn.util.Utils.MovieName;

/**
 * 
 * @author Anh Tran
 *
 */
public class Main {
	
	public static void main(String args[]) throws Exception {
		
		JSAP jsap = new JSAP();
		Switch helpSwitch = new Switch("help")
				.setShortFlag('h')
				.setLongFlag("help");
		FlaggedOption movie = new FlaggedOption("movie")
				.setDefault("")
				.setRequired(true)
				.setLongFlag("movie");
		jsap.registerParameter(helpSwitch);
		jsap.registerParameter(movie);

		JSAPResult config = jsap.parse(args);
		if (config.getBoolean("help") || "".equals(config.getString("movie"))) {
			System.out.print("java -jar social-groups.jar ");
			System.out.println(jsap.getUsage());
			return;
		}
		
		MovieName m = MovieName.fromString(config.getString("movie"));
		
		final String DATA_DIR = "./data/";
		
		File initGroupConfig = new File(DATA_DIR + "cluster/" + config.getString("movie") + "-group.csv");
		if (!initGroupConfig.isFile()) {
			System.err.println("Can't find cluster file: " + initGroupConfig.getAbsolutePath());
			return;
		}
		File pairsFile = new File(DATA_DIR + "pairs/" + config.getString("movie") + ".csv");
		if (!pairsFile.isFile()) {
			System.err.println("Can't find pairs dir: " + pairsFile.getAbsolutePath());
			return;
		}
		
		spatioTemporalGroups(m, initGroupConfig);
		

		File lookatDir = new File(DATA_DIR + "lookat-groundtruth/" + m.toString().toLowerCase());
		if (lookatDir.isDirectory()) {
			spatioTemporalSocialGroups(m, initGroupConfig, lookatDir);
		}
	}

	private static void spatioTemporalGroups(MovieName m, File initGroupConfig) {
		Scene scene = new Scene(m);
		scene.loadGroupConfigurations(initGroupConfig);
		
		File ops = new File(Utils.OUTDIR + scene.getName() + "-ops.txt");
		if (ops.exists()) ops.delete();
		
		int minParticipants = 2;
		
		GroupDefinitions.spatioTemporal(scene, Interval.NOISE_WINDOW, minParticipants);
		Utils.toFile(scene.printGroups(),
				Utils.OUTDIR + scene.getName() + "-spatial.txt", false);
	}
	
	private static void spatioTemporalSocialGroups(MovieName m, File initGroupConfig, File gaze) {
		Scene scene = new Scene(m);
		scene.loadGroupConfigurations(initGroupConfig);
		scene.loadGazeData(gaze);

		File ops = new File(Utils.OUTDIR + scene.getName() + "-ops.txt");
		if (ops.exists()) ops.delete();
		
		int minParticipants = 2;
		float minAttentionRatio = 0.6f;
		
		GroupDefinitions.gazeBased(scene, minAttentionRatio);
		Utils.toFile(scene.printGroups(),
				Utils.OUTDIR + scene.getName() + "-gazebased.txt", false);
		
		GroupDefinitions.spatioTemporal(scene, Interval.NOISE_WINDOW, minParticipants);
		Utils.toFile(scene.printGroups(),
				Utils.OUTDIR + scene.getName() + "-social.txt", false);
	}

}
